package com.tchy.cloud.tools;

import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.youmeng.AliasType;
import org.jeecgframework.youmeng.AndroidNotification;
import org.jeecgframework.youmeng.PushClient;
import org.jeecgframework.youmeng.android.AndroidBroadcast;
import org.jeecgframework.youmeng.android.AndroidCustomizedcast;
import org.jeecgframework.youmeng.ios.IOSBroadcast;
import org.jeecgframework.youmeng.ios.IOSCustomizedcast;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zjy on 2015/12/29.
 * 调用该方法前，请将本机的对外ip加入友盟白名单
 * 发布的时候请将下面注释的setProductionMode打开，setTestMode这个注释
 */
public class YouMengUtil {

    private static Logger log = LoggerFactory.getLogger(YouMengUtil.class);
    //android
    private static String appkey_android = "589adfa0bbea83030800115e";
    private static String appMasterSecret_android = "tcolbwn4vob2qyahe6h5oujzd2ckpowq";
    //ios
    private static String appkey_ios= "589ae1a4b27b0a507e000af2";
    private static String appMasterSecret_ios = "a4ikn6j9r93afcnakhaz79k19s8oumzh";
    private static PushClient client = new PushClient();



    /**
     * 发送系统消息给所有人
     * @param title 通知标题
     * @param text 通知文字描述 400字符内
     * @param openType 打开方式{0:系统消息; 1：百科文章; 2: 新闻文章}
     * @param alias 发送别名(供环信消息使用,如果不发送环信则传null)
     * @param exdParam 扩展参数
     * @return
     */
    public static boolean sendSystemMessage(String title,String text,Integer openType,String alias,Map<String,String> exdParam) throws Exception{
        //android send
        AndroidBroadcast androidBroadcast = new AndroidBroadcast(appkey_android,appMasterSecret_android);
        androidBroadcast.setTicker(title);
        androidBroadcast.setTitle(title);
        androidBroadcast.setText(text);
//        if("1".equals(sendAfter)){//打开activity
//            androidBroadcast.goActivityAfterOpen("pushactivity.ArticleActivity");
//            androidBroadcast.setExtraField("id", id);//id
//            androidBroadcast.setExtraField("object_type", object_type);//类型

            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("avatar","");
//            jsonObject.put("realname","admin");
//            jsonObject.put("user_id","admin");
            jsonObject.put("openType",openType);
            jsonObject.put("exdParam",exdParam);//扩展参数

            androidBroadcast.goCustomAfterOpen(jsonObject);//自定义方式
//        }else{//打开应用
//            androidBroadcast.goAppAfterOpen();
//        }
        androidBroadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // 正式环境用下面这句
        androidBroadcast.setProductionMode();
//        androidBroadcast.setTestMode();
        // Set customized fields
        boolean booandroid = client.send(androidBroadcast);
        //ios send
        IOSBroadcast iosBroadcast = new IOSBroadcast(appkey_ios,appMasterSecret_ios);

        iosBroadcast.setAlert(title);
        iosBroadcast.setBadge(1);
        iosBroadcast.setSound("default");

        // TODO set 'production_mode' to 'true' if your app is under production mode
//        iosBroadcast.setTestMode();
        iosBroadcast.setProductionMode();
        //设置通知参数
//        if("1".equals(sendAfter)){//打开activity
            iosBroadcast.setCustomizedField("openType", openType + "");//类型
            iosBroadcast.setCustomizedField("title",title);
            iosBroadcast.setCustomizedField("content",text);
        if(exdParam != null){
            String object_id = exdParam.get("object_id");//参数ID
            iosBroadcast.setCustomizedField("object_id",object_id);
        }

//        }else{//打开应用
//            iosBroadcast.setCustomizedField("id", "0");//id
//            iosBroadcast.setCustomizedField("object_type", "0");//类型
//            iosBroadcast.setCustomizedField("title",title);
//            iosBroadcast.setCustomizedField("content",text);
//            iosBroadcast.setCustomizedField("object_id",object_id);
//        }
//        iosBroadcast.

        boolean booios = client.send(iosBroadcast);
        if(booandroid || booios){

//        if(booandroid ){
            return true;
        }else{
            return false;
        }
    }



    /**
     * 发送文本消息通知(指定用户推送方式)
     * @param title 通知标题
     * @param text 通知文字描述 400字符内
     * @param alias 发送用户userId，多个用英文逗号分隔（如：1211A0FBC73EB762B416D4B50A9FEB681F,12BEFFACCAF99AFCEFE18712563E49C3C4）
     * @param openType 打开方式{0:系统消息; 1：百科文章; 2: 新闻文章}
     * @param exdParam 扩展参数
     * @return
     * @throws Exception
     */
    public static boolean sendNotificationYouMengAndEasemob(String title,String text,String alias,Integer openType,Map<String,String> exdParam) throws Exception{
        //android send

        AndroidCustomizedcast androidCustomizedcast = new AndroidCustomizedcast(appkey_android,appMasterSecret_android);

        alias = "xinxun_" + alias.replaceAll(",",",xinxun_");

        if(alias.split(",").length < 50){//小于50个人用默认方式发送,大于50则使用fileid的方式发送

            androidCustomizedcast.setAlias(alias, AliasType.XINXUN_CLIENT.getValue());

        }else{
            String fileId = client.uploadContents(appkey_android,appMasterSecret_android,alias);
            androidCustomizedcast.setFileId(fileId, AliasType.XINXUN_CLIENT.getValue());
        }


//        androidCustomizedcast.setTicker(ticker);
        androidCustomizedcast.setTicker(title);
        androidCustomizedcast.setTitle(title);
        androidCustomizedcast.setText(text);

        JSONObject jsonObject = new JSONObject();
//            jsonObject.put("title",title);
//            jsonObject.put("content",text);
//        jsonObject.put("avatar","");
//        jsonObject.put("realname","admin");
//        jsonObject.put("user_id","admin");
        jsonObject.put("openType",openType);
        jsonObject.put("exdParam",exdParam);//扩展参数

        androidCustomizedcast.goCustomAfterOpen(jsonObject);//自定义方式
        androidCustomizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // 正式环境用下面这句
        androidCustomizedcast.setProductionMode();
//        androidCustomizedcast.setTestMode();
        // Set customized fields
        boolean booandroid = client.send(androidCustomizedcast);


        //ios send
        IOSCustomizedcast iosCustomizedcast = new IOSCustomizedcast(appkey_ios,appMasterSecret_ios);

        if(alias.split(",").length < 50){//小于50个人用默认方式发送,大于50则使用fileid的方式发送

            iosCustomizedcast.setAlias(alias, AliasType.XINXUN_CLIENT.getValue());

        }else{
            String fileId = client.uploadContents(appkey_ios,appMasterSecret_ios,alias);
            iosCustomizedcast.setFileId(fileId, AliasType.XINXUN_CLIENT.getValue());
        }

        iosCustomizedcast.setAlert(title);
        iosCustomizedcast.setCustomizedField("content",text);
//        iosCustomizedcast.setDescription(ticker);
        iosCustomizedcast.setBadge( 0);
        iosCustomizedcast.setSound( "default");

        iosCustomizedcast.setCustomizedField("openType", openType + "");//类型
//        iosCustomizedcast.setCustomizedField("title",title);
//        iosCustomizedcast.setCustomizedField("content",text);
        if(exdParam != null){
            String object_id = exdParam.get("object_id");//参数ID
            iosCustomizedcast.setCustomizedField("object_id",object_id);
        }

//		customizedcast.setCustomizedField("","");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        iosCustomizedcast.setProductionMode();
//        iosCustomizedcast.setTestMode();
        boolean booios = client.send(iosCustomizedcast);


        log.debug("推送标题[" + title + "] 推送状态：" + booandroid);

        if(booandroid || booios){
            return true;
        }else{
            return false;
        }
    }


    public static void main(String[] args) {
        try {
            //下面是多个，陈豪你只需要传入一个就好了
//            String alias="1211A0FBC73EB762B416D4B50A9FEB681F,12BEFFACCAF99AFCEFE18712563E49C3C4";
//            alias +=",11oImVDuBIqGRRPFjH_tVy_W67AJDg,11oImVDuHbnQ8cmRMGRYsmcttVu77w";
            String alias2="402881e8576aa78701576b2905f801a6";
//            boolean boo = sendNotificationYouMengAndEasemob("android发送1","android发送2","android发送3",alias2);
            Map<String,String> exdParam = new HashMap<String,String>();
//            exdParam.put("ro","1");//自己角色固定值
//            exdParam.put("object_id",6 + "");//
            exdParam.put("object_id",3832 + "");//
//            boolean boo = sendNotificationYouMengAndEasemob("android发送2","android发送3【这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 " +
//                    "这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 " +
//                    "这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 " +
//                    "这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 " +
//                    "这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 这有五个字 " +
//                    "这有五个字 这有五个字 这有五个字 这有五个字 】",alias2,2,exdParam);

            boolean boo = sendSystemMessage("IOS发送通知&android发送","IOS发送通知内容~~",2,null,exdParam);

            System.out.println("boo=" + boo);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
